package com.css.dzj.dzjportal.modules.jgaction.punishAction.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.aq.server.service.BaseService;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.jgaction.punishAction.vo.Punish;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@PropertySource(value = "classpath:base.properties")
@RequestMapping("/getPunish")
public class PunishController {
    @Autowired
    private HttpServletRequest request;
    @Value("${AP_URL}")
    private String AP_URL;
    @Autowired
    private BaseService baseService;
    /***
     * 多条件查询对外接口
     * @param superviseItemCode
     * @param punishActionName
     * @param caseSource
     * @param punishType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value ="/getAllPage")
    public R getAll(
            @RequestParam(required = false) String superviseItemCode,
            @RequestParam(required = false) String punishActionName,
            @RequestParam(required = false) String caseSource,
            @RequestParam(required = false) String punishType,
            @RequestParam(required = false) Integer pageNo,
            @RequestParam(required = false) Integer pageSize){
        HashMap<String, String> map = new HashMap<>();
        map.put("superviseItemCode", superviseItemCode);
        map.put("punishActionName", punishActionName);
        map.put("caseSource", caseSource);
        map.put("punishType", punishType);
        map.put("pageNo", pageNo.toString());
        map.put("pageSize", pageSize.toString());
        String msg = HttpClientUtil.doGet(AP_URL + "/punish/getAll", map);
        JSONObject jsonObject=JSONObject.parseObject(msg);
        return R.ok().put("data",jsonObject);
    }

    /***
     *查看详情
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public R findById(String id){
        String msg= HttpClientUtil.doGet(AP_URL+"/punish/getPunishById?id="+id);
        JSONObject jsonObject=JSONObject.parseObject(msg);
        return R.ok().put("data",jsonObject);
    }

    /***
     * 获取检查行为总数
     * @return
     */
    @GetMapping("/getNum")
    public R getCount() {
        String msg = HttpClientUtil.doGet(AP_URL + "/punish/countNum");
        Object object = JSON.parse(msg);
        return R.ok().put("data", object);
    }

    /**
     * 导出全部
     * @param
     * @param response
     * @return
     * @throws ParseException
     */
    @GetMapping("/exportAll")
    public R exportAll(@RequestParam(required = false) String superviseItemCode,
                       @RequestParam(required = false) String punishActionName,
                       @RequestParam(required = false) String caseSource,
                       @RequestParam(required = false) String punishType,
                       HttpServletResponse response) throws ParseException {
        HashMap<String, String> map = new HashMap<>();
        map.put("superviseItemCode", superviseItemCode);
        map.put("punishActionName", punishActionName);
        map.put("caseSource", caseSource);
        map.put("punishType", punishType);
        String msg = HttpClientUtil.doGet(AP_URL + "/punish/getAllList", map);
        List<Punish> list = JSON.parseArray(msg, Punish.class);
        export(response,list);
        return R.ok();
    }
    /**
     * 通过ids导出选中数据
     * @param response
     * @param ids
     * @return
     * @throws ParseException
     */
    @GetMapping("/exportByIds")
    public R exportByIds(HttpServletResponse response, String ids){
        String msg = HttpClientUtil.doGet(AP_URL + "/punish/getByPuIds?ids="+ids);
        List<Punish> list=JSON.parseArray(msg, Punish.class);
        export(response,list);
        return R.ok();
    }

    /***
     * 导出逻辑
     * @param response
     * @param list
     */
    private void export(HttpServletResponse response, List<Punish> list) {
        if (list == null || list.size() < 0) {
            throw new RuntimeException("行政处罚行为为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("行政处罚行为监管表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("记录唯一标识");
        titles.add("监管事项目录编码");
        titles.add("处罚行为名称");
        titles.add("处罚行为编号");
        titles.add("实施机构");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (Punish punish : list) {
            ArrayList<Object> row = new ArrayList<>();
            row.add(punish.getRecordUniqueIdentity());
            row.add(punish.getSuperviseItemCode());
            row.add(punish.getPunishActionName());
            row.add(punish.getPunishActionCode());
            row.add(punish.getImplementInstitution());
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "行政处罚行为监管表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
